package com.vgemv.jsutilitysdkdemo.module.Download;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;


//import com.vgemv.jsutilitysdk.Display.JsDisplayManager;
//import com.vgemv.jsutilitysdk.Display.JsDisplayStyle;
import com.vgemv.jsutilitysdk.Download.GajDownloadService;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;

import java.util.ArrayList;

public class DownloadActivity extends AppCompatActivity {


    public Handler mHandler = new Handler(Looper.getMainLooper());
    private GajDownloadService.DownloadBinder downloadBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.v("Name","onServiceConnected");
            downloadBinder = (GajDownloadService.DownloadBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.v("Name","onServiceDisconnected");
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);


        Intent intent = new Intent(this,GajDownloadService.class);
        bindService(intent,connection,BIND_AUTO_CREATE);
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }





        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setId(View.generateViewId());
        setContentView(linearLayout);


        JsMenuFragment gajOneLevelMenuFragment = new JsMenuFragment();
        gajOneLevelMenuFragment.moveToParentView(this,linearLayout);

        gajOneLevelMenuFragment.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                return null;
            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                return null;
            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                ArrayList<JsMenuItemVm> models = new ArrayList<JsMenuItemVm>();
                models.add(new JsMenuItemVmDefault(0,"DisplayLoadingReminder",""));
                models.add(new JsMenuItemVmDefault(1,"UnDisplayLoadingReminder",""));
                models.add(new JsMenuItemVmDefault(2,"消息提示with mask",""));
                models.add(new JsMenuItemVmDefault(3,"消息提示without mask",""));
                models.add(new JsMenuItemVmDefault(4,"消息提示without mask b",""));
                return models;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragmentAlpha, int i, JsMenuItemVm model) {
//                switch (i) {
//                    case 0:{
//                        String url = "http://dubapkg.cmcmcdn.com/duba/166/kinst_166_f13_k0063.exe";
//                        downloadBinder.startDownload(url);
//                        break;
//                    }
//                    case 1:{
//
//                    }
//                    case 2: {
//                        JsDisplayStyle style = JsDisplayStyle.defaultStyle();
//                        style.mb_isNeedMaskView = true;
//                        style.mb_isMaskViewClickCancel = true;
//                        JsDisplayManager.displayMessage("This is a message!",style);
//                        break;
//                    }
//                    case 3: {
//                        JsDisplayManager.displayMessage("This is a message!");
//                        break;
//                    }
//                    case 4: {
//                        Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.mipmap.icon,null);
//                        JsDisplayManager.displayMessage("This is a mesage!",drawable,JsDisplayStyle.defaultStyle());
//                        break;
//                    }
//                    default:{
//                        break;
//                    }
//                }
            }
        };
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length>0&&grantResults[0]!=PackageManager.PERMISSION_GRANTED){
//                        Toast.makeText(this,"拒绝权限将无法使用程序",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}